﻿using System;   
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace iMKelly {
    using CoreClass;
    public partial class frmLending : Form {
        private Database dbs;
        private bool lent = false;
        public frmLending(Database dbs) {
            InitializeComponent();
            this.dbs = dbs;
            exID.Text = dbs.getNextExchangeID();
        }

        private void UserID_TextChanged(object sender, EventArgs e) {
            User user = dbs.findUserByNoID(UserID.Text);
            if (user == null) {
                nameUser.Text = "...";
            }
            else {
                nameUser.Text = user.UserInfo.Name;
                bookID.Focus();
            }
        }

        private void bookID_TextChanged(object sender, EventArgs e) {
            BookTitle title = dbs.findBookByNoID(bookID.Text);
            if (title == null) {
                bookTitle.Text = "...";
            }
            else {
                bookTitle.Text = title.Name;
                if (dbs.checkBookAvailable(bookID.Text)) {
                    statusPic.Image = Properties.Resources.OK;
                    lent = true;
                }
                else statusPic.Image = Properties.Resources.Failed;
                cmdAccept.Focus();
            }
        }

        private void cmdAccept_Click(object sender, EventArgs e) {
            dbs.createExchange(exID.Text, UserID.Text, bookID.Text, lent, "L01");
            Dispose();
        }

        private void button2_Click(object sender, EventArgs e) {
            Dispose();
        }
    }
}
